﻿using System;
using System.Linq;
using BillsForRazorPages.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.Extensions.DependencyInjection;

namespace BillsForRazorPages.Models
{
    public class SeedData
    {
        public static void Initialize(IServiceProvider serviceProvider)
        {
            using (var context = new BillsForRazorPagesContext(serviceProvider
            .GetRequiredService<DbContextOptions<BillsForRazorPagesContext>>()))
            {
                if (context.Bill.Any())
                {
                    return;
                }
                context.Bill.AddRange(
                    new Bill
                    {
                        Content = "测试数据1",
                        Price = 15,
                        BillDate = DateTime.Parse("2020-6-29"),
                        Category = "测试数据",
                        User = "Test"
                    },new Bill
                    {
                        Content = "测试数据2",
                        Price = 18.2m,
                        BillDate = DateTime.Parse("2020-6-29"),
                        Category = "测试数据",
                        User = "Test"
                    }
                );
                context.SaveChanges();
            }
        }
    }
}